const path = require("path");
const htmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require('webpack')

module.exports = {
  mode: "development",
  devtool: "eval-source-map", //开发最佳实践
  //devtool: "source-map", //线上环境最佳实践
  entry: path.join(__dirname, "src/index.js"),
  devServer: {
    contentBase: './dist',
    open: true,
    hot: true,
    hotOnly: true
  },
  module: {
    rules: [
      {
        test: /\.jpe?g|png|gif$/,
        use: {
          loader: "file-loader",
          // url-loader 把图片转成base64
          options: {
            name: "[name]-[hash:8].[ext]",
            outputPath: "images/"
            // limit 2048 将小于2kb的图片转成base64
          }
        }
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      },
      {
        test: /\.scss$/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: 2
            }
          },
          "sass-loader",
          "postcss-loader"
        ]
      }
    ]
  },
  output: {
    publicPath: '/',
    filename: "[name]-[hash:8].js",
    path: path.join(__dirname, "dist"),
    clean: true
  },
  plugins: [
    new htmlWebpackPlugin({ template: "./src/index.html" })
    // new webpack.HotModuleReplacementPlugin()
  ]
};
